using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;

namespace TongyiQianwenapl
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            rtxtanswer.Clear();
            string answer = Postapl(rtxtproblem.Text);
            AIGCTextGenerationStrategy data = JsonConvert.DeserializeObject<AIGCTextGenerationStrategy>(answer);
            rtxtanswer.AppendText($"1.回答text：{data.output.text}\r\n2.停止原因finish_reason：{data.output.finish_reason}\r\n3.总数total_tokens：{data.usage.total_tokens}\r\n4.模型生成回复转换为Token后的长度output_tokens：{data.usage.output_tokens}\r\n5.用户输入文本转换成Token后的长度input_tokens：{data.usage.input_tokens}\r\n6.本次请求的系统唯一码request_id：{data.request_id}");
        }

        private void rtxtproblem_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rtxtproblem.Text.Trim().Length>0)
                {
                    rtxtanswer.Clear();
                    string answer = Postapl(rtxtproblem.Text);
                    AIGCTextGenerationStrategy data = JsonConvert.DeserializeObject<AIGCTextGenerationStrategy>(answer);
                    rtxtanswer.AppendText($"1.回答text：{data.output.text}\r\n2.停止原因finish_reason：{data.output.finish_reason}\r\n3.总数total_tokens：{data.usage.total_tokens}\r\n4.模型生成回复转换为Token后的长度output_tokens：{data.usage.output_tokens}\r\n5.用户输入文本转换成Token后的长度input_tokens：{data.usage.input_tokens}\r\n6.本次请求的系统唯一码request_id：{data.request_id}");
                }
            }
        }
        private string Postapl(string problemText)
        {
            //RestSharp 106.15.0 版本号，用最新版本会报错，也可以自行修改
            var client = new RestClient("https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Authorization", "测试");//API-KEY
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "*/*");
            request.AddHeader("Connection", "keep-alive");
            var body = new JObject {
                { "Content-Type", "application/json" },
                { "Authorization", "测试" },
                { "model", "qwen-turbo" },
                {
                "input", JObject.FromObject(new JObject {
                { "prompt", problemText.Trim()}
                })
                }
                };
            request.AddParameter("application/json", body, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            return response.Content.ToString();
        }

        /// <summary>
        /// 调用结果信息，对于千问模型，包含输出text。
        /// </summary>
        class OutputClass
        {
            //停止原因，null：生成过程中 stop：stop token导致结束 length：生成长度导致结束
            public string finish_reason { get; set; }
            //输出回答
            public string text { get; set; }
        }
        /// <summary>
        /// 计量信息，表示本次请求计量数据。
        /// </summary>
        class UsageClass
        {
            //总数
            public int total_tokens { get; set; }
            //模型生成回复转换为Token后的长度。
            public int output_tokens { get; set; }
            //用户输入文本转换成Token后的长度。
            public int input_tokens { get; set; }
        }

        class AIGCTextGenerationStrategy
        {
            //调用结果信息，对于千问模型，包含输出text。
            public OutputClass output { get; set; }
            //计量信息，表示本次请求计量数据。
            public UsageClass usage { get; set; }
            //本次请求的系统唯一码
            public string request_id { get; set; }
        }


    }
}